package com.easychat.controller;

import com.easychat.entity.enums.ResponseCodeEnum;
import com.easychat.entity.vo.ResponseVo;
import com.easychat.exception.BusinessException;

/**
 * @author Admin
 * @version 1.0
 * @description: TODO
 * @date 2024/6/25 20:19
 */
public class ABaseController {
    protected static final String STATUS_SUCCESS = "success";

    protected static final String STATUS_ERROR = "error";

    protected <T> ResponseVo getSuccessResponseVo(T t){
        ResponseVo<T> responseVo = new ResponseVo<>();
        responseVo.setStatus(STATUS_SUCCESS);
        responseVo.setCode(ResponseCodeEnum.SUCCESS.getCode());
        responseVo.setMsg(ResponseCodeEnum.SUCCESS.getMessage());
        responseVo.setData(t);
        return responseVo;
    }

    protected <T> ResponseVo getBusinessErrorResponseVO(BusinessException e, T t){
        ResponseVo vo = new ResponseVo();
        vo.setStatus(STATUS_ERROR);
        if (String.valueOf(e.getCode()).equals("")){
            vo.setCode(ResponseCodeEnum.CODE_600.getCode());
        }else {
            vo.setCode(e.getCode());
        }
        vo.setMsg(e.getMessage());
        vo.setData(t);
        return vo;
    }

    protected <T> ResponseVo getServerErrorResponseVO(T t){
        ResponseVo vo = new ResponseVo();
        vo.setStatus(STATUS_ERROR);
        vo.setCode(ResponseCodeEnum.CODE_500.getCode());
        vo.setMsg(ResponseCodeEnum.CODE_500.getMessage());
        vo.setData(t);
        return vo;
    }
}
